package io;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: zhenzhong.wang
 * @Time: 2018/4/4 16:17
 */
public class FileCopy {

  /**
   * 读取一个文件中的内容写到另一个文件中
   *
   * @throws IOException
   */
  @Test
  public void copyFileContent() throws IOException {
    FileChannel inCha = new FileInputStream("C:\\Users\\ZZWang\\Desktop\\hac.sql").getChannel();
    FileChannel outCha = new FileOutputStream("C:\\Users\\ZZWang\\Desktop\\hac-bak.sql").getChannel();
    ByteBuffer bb = ByteBuffer.allocate(1024);
    while(inCha.read(bb) != -1) {
      bb.flip();
      outCha.write(bb);
      bb.clear();
    }
  }
}
